package com.tech.nio;

import org.junit.Test;
import java.nio.ByteBuffer;

/**
 * @Description:
 * @Author: zl
 * @Date: 2019/7/5 10:46
 */
public class TestBuffer {

    @Test
    public void test1() {
        //

        String str = "abcde";
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //写数据
        byteBuffer.put(str.getBytes());
        System.out.println("position-1=" + byteBuffer.position());
        System.out.println("limit-1=" + byteBuffer.limit());
        System.out.println("capacity-1=" + byteBuffer.capacity());
        //读数据
        byteBuffer.flip();
//        byte[] bytes = new byte[byteBuffer.limit()];
//        byteBuffer.get(bytes);
//        System.out.println(new String(bytes,0,bytes.length));
        System.out.println("position-2=" + byteBuffer.position());
        System.out.println("limit-2=" + byteBuffer.limit());
        System.out.println("capacity-2=" + byteBuffer.capacity());
        // 重复读
//        byteBuffer.rewind();
//        byteBuffer.clear();
//        byte[] bytes1 = new byte[byteBuffer.limit()];
//        byteBuffer.get(bytes1);
//        System.out.println(new String(bytes1,0,bytes.length));
//        System.out.println(new String(bytes,0,bytes.length));
//        System.out.println("position=" + byteBuffer.position());
//        System.out.println("limit=" + byteBuffer.limit());
//        System.out.println("capacity=" + byteBuffer.capacity());
//        byteBuffer.mark();

    }

    public static void main(String[] args) {
        //test1();
    }
}
